import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {FetchService} from '../../../core/net/fetch.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from "@angular/router";
import {UploadChangeParam, UploadFile} from "ng-zorro-antd/upload";

export interface Data {
  id: number;
  name: string;
}


@Component({
  selector: 'app-examine-index',
  templateUrl: './examine-index.component.html',
  styleUrls: ['./examine-index.component.scss']
})
export class ExamineIndexComponent implements OnInit {
  searchForm1: FormGroup;
  searchForm2: FormGroup;
  tableData: Data[] = [];
  treeData = [
    {
      key: 1,
      name: '水量调度',
      score: 20,
      sq: 1,
      remark: '',
      children: [
        {
          key: 11,
          name: '调度计划执行',
          score: 6,
          sq: 1,
          remark: ''
        },
        {
          key: 12,
          name: '引用水指标的落实',
          score: 8,
          sq: 2,
          remark: ''
        },
        {
          key: 13,
          name: '商品水率',
          score: 8,
          remark: '',
          sq: 3,
        }
      ]
    },
    {
      key: 2,
      name: '节约用水',
      score: 20,
      remark: '',
      sq: 2,
      expand: true,
      children: [
        {
          key: 21,
          name: '调度计划执行',
          score: 7,
          sq: 1,
          remark: ''
        },
        {
          key: 22,
          name: '引用水指标的落实',
          score: 6,
          sq: 2,
          remark: ''
        },
        {
          key: 23,
          name: '商品水率',
          score: 6,
          remark: '',
          sq: 3,
        }
      ]
    },
    {
      key: 3,
      name: '水务服务',
      score: 20,
      remark: '',
      sq: 3,
      expand: true,
      children: [
        {
          key: 31,
          name: '水务信息公开度',
          score: 10,
          sq: 1,
          remark: ''
        },
        {
          key: 32,
          name: '协会管理规范度',
          score: 10,
          sq: 2,
          remark: ''
        }
      ]
    },
    {
      key: 4,
      name: '运行综合',
      score: 20,
      remark: '',
      sq: 4,
      expand: true,
      children: [
        {
          key: 41,
          name: '渠道工程运行管理',
          score: 10,
          sq: 1,
          remark: ''
        },
        {
          key: 42,
          name: '水闸工程运行管理',
          score: 10,
          sq: 2,
          remark: ''
        }
      ]
    },
    {
      key: 5,
      name: '节约能耗',
      score: 20,
      remark: '',
      sq: 5,
      expand: true,
      children: [
        {
          key: 51,
          name: '能源单耗',
          score: 6,
          sq: 1,
          remark: ''
        },
        {
          key: 52,
          name: '机电设备完好率',
          score: 6,
          sq: 2,
          remark: ''
        },
        {
          key: 53,
          name: '机电设备安全运行率',
          score: 8,
          sq: 3,
          remark: ''
        }
      ]
    }
  ];
  treeExpandedData = {};

  tableData2 = [];
  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  rightTableDisplayData = [];
  rightTableData = [];
  delDisable = true;
  rightTableCheckedId: { [key: string]: boolean } = {};
  allChecked = false;
  indeterminate = false;
  numberOfChecked = 0;

  form: FormGroup;
  modalShow = false;
  modalTitle = '';

  form2: FormGroup;
  modal2Show = false;
  modal2Title = '';

  selectedId = '';


  constructor(private fb: FormBuilder,
              private fetchService: FetchService,
              private router: Router,
              private message: NzMessageService) {
    this.searchForm1 = this.fb.group({
      name: [''],
      year: ['2019'],
    });

    this.searchForm2 = this.fb.group({
      name: [''],
    });

    this.form = this.fb.group({
      name: [null],
      score: [null],
      sq: [null],
      remark: [null],
    });

    this.form2 = this.fb.group({
      a: [null],
      b: [null],
      c: [null],
      d: [null],
      e: [null],
      f: [null],
    });
  }

  ngOnInit(): void {
    this.queryInfo();
  }

  collapse(array, data, $event: boolean): void {
    if ($event === false) {
      if (data.children) {
        data.children.forEach(d => {
          const target = array.find(a => a.key === d.key)!;
          target.expand = false;
          this.collapse(array, target, false);
        });
      } else {
        return;
      }
    }
  }

  convertTreeToList(root) {
    const stack = [];
    const array = [];
    const hashMap = {};
    stack.push({ ...root, level: 0, expand: true });

    while (stack.length !== 0) {
      const node = stack.pop()!;
      this.visitNode(node, hashMap, array);
      if (node.children) {
        for (let i = node.children.length - 1; i >= 0; i--) {
          stack.push({ ...node.children[i], level: node.level! + 1, expand: true, parent: node });
        }
      }
    }

    return array;
  }

  visitNode(node, hashMap: { [key: string]: boolean }, array): void {
    if (!hashMap[node.key]) {
      hashMap[node.key] = true;
      array.push(node);
    }
  }

  queryInfo(): void {
    this.treeData.forEach(item => {
      this.treeExpandedData[item.key] = this.convertTreeToList(item);
    });

    this.queryInfo2();
  }

  checkDetail(): void {
    this.fetchService.get<any>(`archives/wrsProjectInfo/${this.selectedId}`).subscribe(res => {
      this.tableData2 = res.projectFiles;
    });
  }

  currentPageDataChange($event): void {
    this.rightTableDisplayData = $event;
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.delDisable = Object.keys(this.rightTableCheckedId).length == 0;
    this.isAllDisplayDataChecked = this.rightTableDisplayData
      .every(item => this.rightTableCheckedId[item.id]);
    this.isIndeterminate =
      this.rightTableDisplayData.some(item => this.rightTableCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
    this.numberOfChecked = this.rightTableData.filter(item => this.rightTableCheckedId[item.id]).length;
  }

  checkAll(value: boolean): void {
    this.rightTableDisplayData.forEach(item => (this.rightTableCheckedId[item.id] = value));
    this.refreshStatus();
  }

  openEditModal(item?): void {
    this.modalTitle = item ? `编辑` : '新增';
    this.form.patchValue(item || {});
    this.modalShow = true;
  }

  onAdd(): void {
    this.form.reset();
    this.openEditModal2();
  }

  onDelete(id): void {
    console.log(id);
  }

  onCancel() {
    this.modalShow = false;
  }

  onDeleteAll(): void {
    const ids = Object.keys(this.rightTableCheckedId);
  }

  onConfirm(): void {

  }

  queryInfo2(): void {
    this.rightTableData = [
      {a: '水量调度', b: '调度计划执行', c: 8, d: 1, e: '', f: ''},
      {a: '水量调度', b: '引用水指标的落实', c: 6, d: 2, e: '', f: ''},
      {a: '水量调度', b: '商品水率', c: 6, d: 3, e: '', f: ''},
    ];
  }

  openEditModal2(item?): void {
    this.modal2Title = item ? `编辑` : '新增';
    this.form2.patchValue(item || {});
    this.modal2Show = true;
  }

  onAdd2(): void {
    this.form2.reset();
    this.openEditModal2();
  }

  onDelete2(id): void {
    console.log(id);
  }

  onCancel2() {
    this.modal2Show = false;
  }

  onDeleteAll2(): void {
    const ids = Object.keys(this.rightTableCheckedId);
  }

  onConfirm2(): void {

  }

}
